<?php
declare (strict_types = 1);

namespace app\controller\api\v1\user\region;

use app\controller\api\ApiController;
use think\App;
use think\response\Json;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use app\service\logic\backend\region\RegionService;

class RegionController extends ApiController
{
    protected $service;

    public function __construct(App $app, RegionService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function chained()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $search['p_code'] = !isset($search['p_code']) || empty($search['p_code']) ? '0' : $search['p_code'];
        $params['search'] = '不限';

        $result = $this->service->getOptions($params, $search_fields, $search);
        $result = empty($result) ? null : $result;
        return amis($result);
    }

    /**
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read()
    {
        $params = $this->request->param();
        $result = $this->service->getSimpleDetail($params);
        return amis($result);
    }
}
